<?php
/**
 * a class that can contains fily_ControlToHtml members
 */
fily_XMLConverter::registerNamespace('controllist', fily_ControlList);
class fily_ControlList implements fily_ChildControl, fily_XMLToControl {
    /**
     * items of the list
     * @var array
     */
    protected $items;

    /**
     * is set to true when the code is prepared
     * @var boolean
     */
    protected $prepared = false;

    /**
     * parent from this control
     * @var fily_ControlToHtml
     */
    protected $parent;
    /**
     * returns the parent from this control
     * @return fily_ControlToHtml
     */
    public function getParent() {
        return $this->parent;
    }

    /**
     * system id
     * @var string
     */
    protected $sid;

    /**
     * system id
     * @return string
     */
    public function getSid(){
        return $this->sid;
    }

    /**
     * system id
     * @param string $sid
     */
    public function setSid($sid){
        $this->sid = $sid;
    }




    /**
     * set the parent from this control
     * @param fily_ControlToHtml $parent
     */
    public function setParent(fily_ControlToHtml $parent = null) {
        $this->parent = $parent;
    }

    /**
     * returns the id from this control
     * @return string
     */
    public function getId() {
        $id = "";
        if($this->getParent()!=null) {
            $id = $this->getParent()->getId();
        }
        return $id;
    }

    /**
     * you can hand over multiple fily_ControlToHtml parameters
     */
    public function __construct() {
        $this->items = array();
        $args = func_get_args();
        foreach($args as $arg) {
            if($arg instanceof fily_ControlToHtml) {
                $this->add($arg);
            }
        }
    }

    /**
     * adds a fily_ControlToHtml control
     * @param fily_ControlToHtml $fily_Control
     */
    public function add(fily_ChildControl $control) {
        $control->setParent($this);
        $this->items[] =  $control;
    }

    /**
     * removes the list item
     * @param string $id
     */
    public function remove(fily_ChildControl $object) {
        foreach ($this->items as $id => $value) {
            if($value==$object) {
                unset($this->items[$id]);
                break;
            }
        }
    }

    /**
     * returns a item
     * @param int $id
     * @return fily_ControlToHtml
     */
    public function get($id) {
        $listEntry = null;
        if($id >= 0 && $id < $this->count()) {
            $listEntry = $this->items[$id];
        }
        return $listEntry;
    }

    /**
     *  converts all entries of the list into fily_Html
     * @return string
     */
    public function toHtml() {
        if(!$this->prepared) {
            $this->prepare();
        }
        $html = '';
        foreach($this->items as $control) {
            if($control!=null) {
                $html .= $control->toHtml();
            }
        }
        $this->prepared = false;
        return $html;
    }

    /**
     * shortcut for toHtml()
     */
    public function __toString() {
        return $this->toHtml();
    }

    /**
     * prepares the code for the html output
     */
    public function prepare() {
        foreach($this->items as $control) {
            if($control!=null) {
                $control->prepare();
            }
        }
        $this->prepared = true;
    }


    /**
     * returns the count of all list entries
     * @return int
     */
    public function count() {
        return count($this->items);
    }

    /**
     * clears the list
     */
    public function clear() {
        $this->items = array();
    }


    /**
     * checks if the list is empty
     * @return boolean
     */
    public function isEmpty() {
        return ($this->count() == 0);
    }

    /**
     * returns the list items
     * @return array
     */
    public function getItems() {
        return $this->items;
    }


    /**
     * finds a control with the specific id
     * @param string $id
     * @return fily_ControlToHtml
     */

    public function getControlById($id) {
        $searchedControl = null;
        foreach($this->items as $control) {
            if($control->getId()==$id) {
                $searchedControl = $control;
            }
            else {
                $searchedControl = $control->getControlById($id);
            }
            if($searchedControl!=null) {
                break;
            }
        }
        return $searchedControl;
    }


     /**
     * finds a control with the specific system id
     * @param string $sid
     * @return fily_ControlToHtml
     */

    public function getControlBySid($sid) {
        $searchedControl = null;
        foreach($this->items as $control) {
            if($control->getSid()==$sid) {
                $searchedControl = $control;
            }
            else {
                $searchedControl = $control->getControlBySid($sid);
            }
            if($searchedControl!=null) {
                break;
            }
        }
        return $searchedControl;
    }




    public function find($expression) {
        return $this->parent->find($expression);
    }

    public function getAfterId($id) {
        $control = null;
        // step through the items and search the item after given id
        $isAfter = false;
        foreach ($this->items as $item) {
            if($item->getId()== $id) {
                $isAfter = true;
            }
            else if($isAfter) {
                $control = $item;
            }
        }
        return $control;
    }

    public function getBeforeId($id) {
        $control = null;
        // step through the items and search the item before given id
        $lastItem = null;
        foreach ($this->items as $item) {
            if($item->getId()== $id) {
                $control = $lastItem;
            }
            $lastItem = $item;

        }
        return $control;
    }
    public function XMLAddControl(fily_ControlToHtml $control) {
        $this->add($control);
    }
    public function XMLAttributes(array $attributes, $innerValue) {
    }
    public function XMLCallFunction($name, array $parameters, $innerValue, fily_ControlToHtml $innerControl = null) {
    }
    public function XMLLateBinding(fily_ControlToHtml $baseControl) {
    }

}


?>